
const PROFILE_TOKEN_ERROR = 'ProfileToken cannot null!';
const FN_SUCCESS = fnName => `The ${fnName} function is success!`;
function control(device) {
    const PTZ = device.services.ptz;
    const ptzMove = async (speed, timeout) => {
        speed = speed || { x: 0, y: 0, z: 0 };
        timeout = timeout || 1;
        try {
            await device.ptzMove({ speed, timeout })
            return { type: 'success', message: FN_SUCCESS('ptzMove') }
        } catch (error) {
            return { type: 'error', message: error.message }
        }
    };
    const ptzStop = async () => {
        try {
            await device.ptzStop()
            return { type: 'success', message: FN_SUCCESS('ptzStop') }
        } catch (error) {
            return { type: 'error', message: error.message }
        }
    };
    const continuousMove = async (params) => {
        let { ProfileToken, Velocity, Timeout } = params;
        if (!ProfileToken) return { type: 'error', message: PROFILE_TOKEN_ERROR };
        if (JSON.stringify(Velocity) === '{}') Velocity = { x: 0, y: 0, z: 0 };
        if (!Timeout) Timeout = 1;
        try {
            await PTZ.continuousMove({ ProfileToken, Velocity, Timeout })
            return { type: 'success', message: FN_SUCCESS('continuousMove') }
        } catch (error) {
            return { type: 'error', message: error.message }
        }
    };
    const absoluteMove = async (params) => {
        let { ProfileToken, Position, Speed } = params;
        if (!ProfileToken) return { type: 'error', message: PROFILE_TOKEN_ERROR };
        if (JSON.stringify(Position) === '{}') Position = { x: 0, y: 0, z: 0 };
        if (JSON.stringify(Speed) === '{}') Speed = { x: 0, y: 0, z: 0 };
        try {
            await PTZ.absoluteMove({ ProfileToken, Position, Speed })
            return { type: 'success', message: FN_SUCCESS('absoluteMove') }
        } catch (error) {
            return { type: 'error', message: error.message }
        }
    };
    const relativeMove = async (params) => {
        let { ProfileToken, Translation, Speed } = params;
        if (!ProfileToken) return { type: 'error', message: PROFILE_TOKEN_ERROR };
        if (JSON.stringify(Translation) === '{}') Translation = { x: 0, y: 0, z: 0 };
        if (JSON.stringify(Speed) === '{}') Speed = { x: 0, y: 0, z: 0 };
        try {
            await PTZ.relativeMove({ ProfileToken, Translation, Speed })
            return { type: 'success', message: FN_SUCCESS('relativeMove') }
        } catch (error) {
            return { type: 'error', message: error.message }
        }
    };
    const stop = async (params) => {
        let { ProfileToken, PanTilt, Zoom } = params;
        if (!ProfileToken) return { type: 'error', message: PROFILE_TOKEN_ERROR };
        if (!PanTilt) PanTilt = true;
        if (!Zoom) Zoom = true;
        try {
            await PTZ.stop({ ProfileToken, PanTilt, Zoom })
            return { type: 'success', message: FN_SUCCESS('stop') }
        } catch (error) {
            return { type: 'error', message: error.message }
        }
    };
    const deviceMove = (params) => {
        if (!params.type || !params.orient || JSON.stringify(params.orient) === {}) return;
        if (params.type === 'absolute') {
            params.Position = params.orient || { x: 0, y: 0, z: 0 };
        } else if (params.type === 'relative') {
            params.Translation = params.orient || { x: 0, y: 0, z: 0 };
        } else if (params.type === 'continuous') {
            params.Velocity = params.orient || { x: 0, y: 0, z: 0 };
        }
        delete params.orient;
        const moveFn = control(device)[params.type + 'Move'].bind(this);
        delete params.type;
        return Promise.resolve(moveFn(params));
    };
    return {
        ptzMove,
        ptzStop,
        continuousMove,
        absoluteMove,
        relativeMove,
        stop,
        deviceMove
    }
}
module.exports = control;